//
//  NotificationService.m
//  MyServiceExtension
//
//  Created by 熊宏伟 on 2018/2/9.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "NotificationService.h"
#import <AudioToolbox/AudioToolbox.h>

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    NSDictionary *dic = self.bestAttemptContent.userInfo;
    BOOL is_voice = (BOOL)dic[@"aps"][@"iswurao"];
//    // Modify the notification content here...
//    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
    BOOL newMsgAlert = NO;  //新消息提醒
    BOOL sound = NO;      //声音
    BOOL shake = NO;      //震动
    
    NSString* msgSetting = dic[@"aps"][@"msg_setting"];;
    NSString* newMsgAlertStr = [msgSetting substringToIndex:1];
    NSString* soundStr = [msgSetting substringWithRange:NSMakeRange(1, 1)];
    NSString* shakeStr = [msgSetting substringWithRange:NSMakeRange(2, 2)];
    //    NSString* alertAndShowMsgContentStr = [msgSetting substringFromIndex:2];
    
    NSLog(@"远程推送extension->dic:%@",dic);
    NSLog(@"远程推送extension->is_voice:%@",is_voice);
    if ([newMsgAlertStr isEqualToString:@"1"]) {
        newMsgAlert = YES;
    }
    else {
        newMsgAlert = NO;
    }
    if ([soundStr isEqualToString:@"1"]) {
        sound = YES;
    }
    else {
        sound = NO;
    }
    if ([shakeStr isEqualToString:@"1"]) {
        shake = YES;
    }
    else {
        shake = NO;
    }
    if (!sound && shake) {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        
        self.bestAttemptContent.body = @"熊宏伟";
        self.contentHandler(self.bestAttemptContent);
    }
    else {
        self.bestAttemptContent.body = @"熊宏伟";
        self.contentHandler(self.bestAttemptContent);
    }
    
    

}

- (void)serviceExtensionTimeWillExpire {
    // Called just before the extension will be terminated by the system.
    // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
    self.contentHandler(self.bestAttemptContent);
}

@end
